import request from '@/utils/request'
import ApolloClient from "apollo-boost";
import gql from "graphql-tag";
const group_name = 'rent'
const api_name = 'oldhouse'

const client = new ApolloClient({
  uri: "http://127.0.0.1:18080/graphql"
});

//定义查询
const QUERY_LIST = gql`
query HouseResourcesList($pageSize: Int, $page: Int, $resourceType:Int) {
  HouseResourcesList(pageSize: $pageSize, page: $page, resourceType: $resourceType) {
    list {
      id
      pic
      title
      coveredArea
      orientation
      floor
      rent
      resourceType
      houseType
      city
      county
      
    }
    ,
    pagination{
      current
      pageSize
      total
    }
  }
}
`;

const QUERY_HOUSE = gql`
    query HouseResources($id :Long) {
        HouseResources(id: $id) {
           
                id
                pic
                title
                coveredArea
                orientation
                floor
                rent
                resourceType
                contact
                mobile
                estateId
                buildingNum
                buildingUnit
                propertyCost
                buildingFloorNum
                houseType
                useArea
                city
                county
                intentions
                address
                facilities
                
        }
    }
`;


//定义查询
const GET_MAP_HOUSE = gql`
    query MapHouseData($lng: Float, $lat: Float, $zoom: Int) {
        MapHouseData(lng: $lng, lat: $lat, zoom: $zoom) {
            list {
                x
                y
            }
        }
    }
`;

export default {
  getList() {
    return request({
      url: `/${api_name}`,
      method: 'get'
    })
  },
  search(page, size,searchMap) {
    // return request({
    //   url: `/${api_name}/search/${page}/${size}`,
    //   method: 'post',
    //   data: searchMap
    // })
    console.log(searchMap.resourceType);

    return  client.query({query: QUERY_LIST, variables: {"pageSize":size,"page":page,"resourceType":2}})
  },
  save(pojo) {
    return request({
      url: `/${api_name}`,
      method: 'post',
      data: pojo
    })
  },
  findById(id) {
    return request({
      url: `/${api_name}/${id}`,
      method: 'get'
    })
  },
  update(id, pojo) {
    if (id === null || id === '') {
      return this.save(pojo)
    }
    return request({
      url: `/${api_name}/${id}`,
      method: 'put',
      data: pojo
    })
  },
  deleteById(id) {
    return request({
      url: `/${api_name}/${id}`,
      method: 'delete'
    })
  },
  count() {
    return request({
      url: `/${api_name}/count`,
      method: 'get'
    })
  }
  ,

  queryMapHouse(lng,lat,zoom){

   return client.query({query: GET_MAP_HOUSE, variables:{"lng":lng,"lat":lat,"zoom":zoom}})


  }
}
